import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;


public abstract class DAO {

//método executeSQL
	protected int executeSQL(String sql) {
		int r = 0;
		Connection con = null;
		Statement st = null;

		try {
			con = DBConnection.getConnection();
			st = con.createStatement();

			r = st.executeUpdate(sql);


		} catch (SQLException e) {
			System.out.println("erro "+e);
		} finally {
			DBConnection.closeStatement(st);
			DBConnection.closeConnection(con);

		}

		return r;
	}


	protected Collection executeQuery(String sql) {
		ResultSet rs = null;
		Connection con = null;
		Statement st = null;
		ArrayList al = new ArrayList();

		try {
			con = DBConnection.getConnection();
			st = con.createStatement();
            //obtém a Query, mas não sabe sobre os campos da TABELA.
			rs = st.executeQuery(sql);

			while (rs.next()) {
				al.add(createObject(rs));
			}
		} catch (SQLException e) {
			System.out.println("erro "+e);
		} finally {
			DBConnection.closeResultSet(rs);
			DBConnection.closeStatement(st);
			DBConnection.closeConnection(con);
		}

		return al;
	}

   //método abstrato
	protected abstract Object createObject(ResultSet rs) throws SQLException;

}
